import React, { Component } from 'react';
import { Redirect } from "react-router-dom";
import { connect } from 'react-redux';
import {fromJS} from 'immutable';
import { actionCreators } from './store';
import { LoginWrap } from './style';
import LoginType from './components/loginType';
import CodeLogin from './components/codeLogin';
import PsdLogin from './components/psdLogin';
import {  Link } from "react-router-dom";
class LoginForm extends Component {
    render() {
        const { type ,isLogin } = this.props;
        if (!isLogin) {
            const { login } = this.props;
            return (
                <LoginWrap>
                    <LoginType />
                    {type === "psdLogin" ? <PsdLogin loginHandle={login} /> : <CodeLogin loginHandle={login} />}
                    <div className="info">
                        <Link to='/'>忘记密码？</Link>
                        <Link to='/register'>注册账号</Link>
                    </div>
                </LoginWrap>
            )
        } else {
            return <Redirect to="/home" />;
        }
    }
}
const mapStateToProps = (state, ownProps) => {
    state= fromJS(state);
    return {
        type: state.getIn(["login", 'type']),
        isLogin: state.getIn(["login", "isLogin"]),
    }
}
const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        login(val) {
            dispatch(actionCreators.login(val))
        },
        loginHandle(isLogin){
          dispatch(actionCreators.loginHandle(isLogin))  
        }
    }
}
export default connect(mapStateToProps, mapDispatchToProps)(LoginForm)
